﻿using System;
using System.ComponentModel.Composition;
using System.Windows;
using Microsoft.Expression.Extensibility;
using Microsoft.Expression.Framework.Commands;
using Microsoft.Expression.Framework.UserInterface;
using Oxage.Rasterizer.Views;

namespace Oxage.Rasterizer
{
	[Export(typeof(IPackage))]
	public class RasterAddIn : CommandTarget, IPackage
	{
		private IServices services;
		private RasterView view;

		public void Initialize()
		{
			try
			{
				var commandService = services.GetService<ICommandService>();
				var windowService = services.GetService<IWindowService>();

				var contractService = new RasterContractService(services);

				this.view = new RasterView();
				this.view.ViewModel.RegisterContractService(contractService);

				commandService.AddTarget(this);

				if (windowService.PaletteRegistry["xbraster"] == null)
				{
					windowService.RegisterPalette("xbraster", this.view, "Raster");
					windowService.PaletteRegistry["xbraster"].IsVisible = true;
				}
			}
			catch (Exception ex)
			{
				if (Settings.Debug)
				{
					MessageBox.Show(ex.Message, "Error", MessageBoxButton.OK, MessageBoxImage.Error);
				}
			}
		}

		public void Load(IServices services)
		{
			this.services = services;
			Initialize();
		}

		public void Unload()
		{
		}
	}
}